<template>
  <div class="goods-cate">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
      <el-breadcrumb-item to="/goods">商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="addDialog.visible = true"
            >添加分类</el-button
          >
        </el-col>
      </el-row>

      <el-table
        border
        stripe
        :data="list"
        row-key="cat_id"
        lazy
        :load="onLoadChildren"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="分类名称" prop="cat_name"></el-table-column>
        <el-table-column label="是否有效" prop="cat_deleted">
          <template #default="{ row }">
            <i
              v-if="!row.cat_deleted"
              class="el-icon-success"
              style="color: #39ce39;"
            ></i>
            <i v-else class="el-icon-error" style="color: #f56c6c;"></i>
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="cat_level">
          <template #default="{ row }">
            <el-tag v-if="row.cat_level === 0" type="primary">一级</el-tag>
            <el-tag v-else-if="row.cat_level === 1" type="success">二级</el-tag>
            <el-tag v-else type="warning">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default>
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.current"
        :page-sizes="[5, 10, 15, 20, 50]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      >
      </el-pagination>
    </el-card>

    <el-dialog
      title="添加商品分类"
      :visible.sync="addDialog.visible"
      width="50%"
    >
      <template #default>
        <el-form
          ref="addForm"
          label-width="80px"
          :model="addDialog.form.data"
          :rules="addDialog.form.rules"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model.trim="addDialog.form.data.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <goods-cate-cascader
              v-model="addDialog.form.data.parentGoodsCate"
              :showLevel="2"
              style="width: 100%;"
            ></goods-cate-cascader>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <el-button @click="addDialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// @ --> /src
import GoodsCateCascader from '@/components/GoodsCateCascader.vue';

export default {
  name: 'GoodsCate',
  components: { GoodsCateCascader },
  data() {
    return {
      addDialog: {
        visible: false,
        form: {
          data: { cat_name: '', parentGoodsCate: [] },
          rules: {
            cat_name: [
              { required: true, message: '请输入分类名称', trigger: 'change' },
            ],
          },
        },
      },
      list: [],
      page: {
        current: 1,
        size: 10,
        total: 0,
      },
    };
  },
  methods: {
    onChangeOfGoodsCate(value) {
      console.log(value, '商品分类页面监听到了 GoodsCateCascader 组件的 change 事件');
    },
    add() {
      this.addDialog.visible = false;
    },
    handleSizeChange(size) {
      this.page.size = size;
      this.page.current = 1;
      this.getList();
    },
    handleCurrentChange(current) {
      this.page.current = current;
      this.getList();
    },
    // 当点击每一行记录前的大于号时（展开时），会触发这个函数
    async onLoadChildren(tree, treeNode, resolve) {
      console.log('onLoadChildren', tree, treeNode, resolve);
      const { data: res } = await this.__apis.goodsCate.getListByPid(
        tree.cat_id
      );
      res.data.forEach((v) => (v.hasChildren = v.cat_level !== 2));
      resolve(res.data);
    },
    async getList() {
      const { data: res } = await this.__apis.goodsCate.list({
        type: 1,
        pagenum: this.page.current,
        pagesize: this.page.size,
      });
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }

      res.data.result.forEach((v) => (v.hasChildren = true));

      this.list = res.data.result;

      this.page.total = res.data.total;
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.goods-cate {
  .el-card {
    margin-top: 20px;
    .el-table,
    .el-pagination {
      margin-top: 20px;
    }
  }
}
</style>
